package com.example.waibao.service.impl;

import com.example.waibao.dao.UserMapper;
import com.example.waibao.entity.LoginUser;
import com.example.waibao.entity.User;
import com.example.waibao.mapper.MenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Objects;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        //查询用户权限


        String[] split = username.split(",");
        String username2 = split[0];
        String school_id = split[1];
        System.out.println(split[0]);
        System.out.println(split[1]);
        System.out.println("_____————————————_____");
        User user = userMapper.selectByUserName(username2, Integer.valueOf(school_id));
        System.out.println("————————————");
        System.out.println(username);
        if(Objects.isNull(user)){
            throw new RuntimeException("用户名或者密码错误");
        }
        //TODO 查询用户权限
        System.out.println(user.getId());
     //   List<String> list = new ArrayList<>(Arrays.asList("test","admin"));
        List<String> list = menuMapper.select_perms_by_user_id(user.getId());
        //把数据封装成UserDetails返回
        return new LoginUser(user,list);
    }


}
